package com.tf_cut.backend.service.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.service.service.ServiceRefSellerService;
import com.tf_cut.backend.service.vo.ServiceRefSellerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/backend/service/ref")
@Slf4j
public class ServiceRefController {

	@Autowired
	private ServiceRefSellerService serviceRefSellerService;

	/**
	 * 查找关联商户与服务商
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping("findListServiceRefSeller")
	public String findListSeller(String json) {
		ServiceRefSellerVo avo = JSON.parseObject(json, ServiceRefSellerVo.class);
		return JSON.toJSONString(serviceRefSellerService.findListServiceRefSeller(avo));
	}

	@RequestMapping("getSellerRef")
	public String getSellerRef(String id) {
		Result<ServiceRefSellerVo> result = serviceRefSellerService.get(id);
		return JSON.toJSONString(result.getData());
	}

	@RequestMapping("saveSellerRefService")
	public String saveSellerRefService(String json) {
		Result<ServiceRefSellerVo> result = null;
		ServiceRefSellerVo avo = JSON.parseObject(json, ServiceRefSellerVo.class);
		Integer serviceSellerNum = serviceRefSellerService.findServiceSellerNum(avo.getServiceId(), avo.getSellerId());
		if (serviceSellerNum > 0) {
			result = Result.of(avo);
			result.setMsg("该商户已关联该服务商");
			result.setFlag(false);
		} else {
			try {
				result = serviceRefSellerService.save(avo);
			} catch (Exception e) {
				log.error("saveSellerRefService error:{},sellerRefService param={}", e.getMessage(), json, e);
				result = Result.of(avo);
				result.setMsg(e.getMessage());
				result.setFlag(false);
			}
		}
		return JSON.toJSONString(result);
	}

	@RequestMapping("operation")
	public String operation(String id, String state) {
		Result<Boolean> result = Result.of(true);
		result.setFlag(true);
		try {
			serviceRefSellerService.delete(id);
		} catch (Exception e) {
			log.error("serviceRefSeller operation error:{},id={}", e.getMessage(), id, e);
			result = Result.of(false);
			result.setFlag(false);
		}
		return JSON.toJSONString(result);
	}

}
